﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CFR.Silverlight.Scheduling
{
    public class AgendaView : ViewBase
    {
        protected internal ItemsControl ScheduleItemsControl { get; private set; }

        public class AgendaDay
        {
            public DateTime Date { get; set; }
            public List<EventItem> Events { get; set; }
        }

        public AgendaView(Scheduler scheduler)
            : base(scheduler)
        {
            this.DefaultStyleKey = typeof(AgendaView);

            this.Loaded += new RoutedEventHandler(AgendaView_Loaded);
        }



        void AgendaView_Loaded(object sender, RoutedEventArgs e)
        {
            ApplyTemplate();
            Refresh();
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ScheduleItemsControl = (ItemsControl)GetTemplateChild("ScheduleItemsControl");


        }

        public void RefreshEvents()
        {
            if (ScheduleItemsControl == null)
                return;

            var events = Scheduler
                .Events.Where(ee => ee.Calendar.IsShownInScheduler);

            List<AgendaDay> list = new List<AgendaDay>(Scheduler.AgendaDayCount);

            for (int i = 0; i < Scheduler.AgendaDayCount; i++)
            {
                list.Add(new AgendaDay()
                {
                    Date = Scheduler.StartDate.AddDays(i),
                    Events = events
                                .Where(e => e.StartDate >= Scheduler.StartDate.Date.AddDays(i) && e.StartDate < Scheduler.StartDate.Date.AddDays(i + 1))
                                .OrderBy(e=>e.StartDate)
                                .Select(e => new EventItem() { Event = e, 
                                    Background = e.Calendar.Background, 
                                    Title = e.Name, 
                                    View = this, 
                                    Foreground=new SolidColorBrush(Colors.White),
                                    Margin = new Thickness(2,1,2,1),
                                    IsDraggable = false,
                                    TitleContentTemplate = Scheduler.EventTitleContentTemplate
                                })
                                .ToList(),
                    
                });
            }

            if (Scheduler.EventTooltip != null || Scheduler.AllDayEventTooltip != null)
            {
                foreach (var ad in list)
                {
                    ad.Events = ad.Events.OrderByDescending(e => e.Event.IsAllDayEvent).ToList();

                    foreach (var ei in ad.Events)
                    {
                        if (ei.Event.IsAllDayEvent && Scheduler.AllDayEventTooltip != null)
                            ToolTipService.SetToolTip(ei, new ContentControl() { Content = ei.Event, ContentTemplate = Scheduler.AllDayEventTooltip });
                        else if (!ei.Event.IsAllDayEvent && Scheduler.EventTooltip != null)
                            ToolTipService.SetToolTip(ei, new ContentControl() { Content = ei.Event, ContentTemplate = Scheduler.EventTooltip });


                    }
                }
            }

            ScheduleItemsControl.ItemsSource = list;
        }

        protected internal override void Refresh()
        {
            base.Refresh();

            RefreshEvents();
        }

    }
}
